<template>
  <div>
    <div class="detail_box">
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>管理员名称：</label>
            <el-input type="text" placeholder="请输入管理员名称" v-model.trim="username"></el-input>
          </div>
          <div class="operate_item">
            <label>手机号：</label>
            <el-input type="text" placeholder="请输入手机号" v-model.trim="phone"></el-input>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
        <div class="btn_box" v-if="btn&&btn.add">
          <span class="operate_btn search_btn" @click="openPopup('新增','')">新增</span>
        </div>
      </div>
      <div class="table_box">
        <el-table
          border
          v-loading="loading"
          :header-cell-style="tableHeader"
          ref="multipleTable"
          :data="tableInfo"
          tooltip-effect="dark"
          style="width: 100%;">
          <el-table-column
            width="95"
            prop="id"
            label="ID">
          </el-table-column>
          <el-table-column
            prop="modifytime"
            sortable
            label="修改时间">
            <template slot-scope="scope">{{ transferTime(scope.row.modifytime) }}</template>
          </el-table-column>
          <el-table-column
            prop="name"
            label="名称">
          </el-table-column>
          <el-table-column
            prop="phone"
            label="电话">
          </el-table-column>
          <el-table-column
            label="操作"
            v-if="btn&&(btn.bindBuilding||btn.update||btn.delete)"
            fixed="right"
            width="200">
            <template slot-scope="scope">
              <div class="table_btn">
                <span class="change_info" @click="openPopup('修改',scope.row)" v-if="btn.update">修改</span>
                <span class="change_info" @click="bindEvents(scope.row)" v-if="btn.bindBuilding">绑定楼宇</span>
                <span class="change_info delete_btn" @click="deleteItem(scope.row.id)" v-if="btn.delete">删除</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="record_page">
        <span class="page_count">共<i>{{ totals }}</i>条</span>
        <el-pagination
          @size-change="sizeChange"
          @current-change="getTableInfo"
          background
          :current-page="currentPage"
          :page-sizes="[15, 20, 30, 40]"
          :page-size="pageSize"
          layout="prev, pager, next, sizes,jumper"
          :total="totals">
        </el-pagination>
      </div>
    </div>
    <!-- 窗口 -->
    <edit v-if="isPop" :manager="manager" @listenToChild="listenToEdit"/>
    <bindEvent v-if="isBind" :eid="eventId" :ids="events" :type="eType" @listenToEvent="listenToEdit"/>
  </div>
</template>

<script>

import bindEvent from "./bindEvent";
import edit from "./edit";
import commons from "@/utils/common";
import {getBtn} from "@/utils/btnAuth";
import {getFireBuildingManager, deleteFireBuildingManager} from "../../../api/manager";

export default {
  components: {
    bindEvent,
    edit
  },
  data() {
    return {
      username: '',
      phone: '',
      tableInfo: [],
      currentPage: 1,
      pageSize: 15,
      totals: 0,
      loading: false,
      isBind: false,
      eventId: '',
      events: '',
      eType: 1,
      isPop: false,
      manager: '',
      btn: {}
    }
  },
  mounted() {
    this.btn = getBtn();
    if (this.btn.query) {
      this.getTableInfo(1);
    }
  },
  methods: {
    // 获取列表信息
    getTableInfo(page) {
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        limit: that.pageSize,
        page: page,
        phone: that.phone,
        name: that.username
      }
      getFireBuildingManager(queryParams).then(res => {
        that.tableInfo = res.data ? res.data.list : [];
        that.totals = res.data ? res.data.count : 0;
        that.loading = false;
      }, (err) => {
        that.loading = false;
      });
    },

    resetTable() {
      this.pageSize = 15;
      this.username = '';
      this.phone = '';
      this.getTableInfo(1);
    },

    // 绑定
    bindEvents(obj) {
      this.eventId = obj ? obj.id : '';
      this.events = obj && obj.buildingIds ? obj.buildingIds : '';
      this.isBind = true;
    },

    listenToEdit(e) {
      this.isBind = false;
      this.isPop = false;
      if (e.refresh) {
        this.getTableInfo(this.currentPage);
      }
    },

    openPopup(obj) {
      this.isPop = true;
      if (obj) {
        this.manager = obj;
      }
    },

    // 删除
    deleteItem(id) {
      let that = this;
      that.$confirm('确定删除该管理员信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.loading = true;
        deleteFireBuildingManager(id).then(res => {
          if (res.success) {
            that.getTableInfo(that.currentPage);
            that.$message.success('删除成功！');
          }
          that.loading = false;
        }, (err) => {
          that.loading = false;
          that.$message.error('删除失败，请重试');
        });
      }).catch(() => {
      });
    },

    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1)
    },

    transferTime(time) {
      return commons.formatTime(time)
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
    },
  }
}
</script>

<style scoped>
.box_body {
  height: 300px;
}
</style>
